package dice.sinanya.tools.getinfo;

import dice.sinanya.entity.EntityTypeMessages;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;

import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.makedata.Sender.sender;

public class MyDownLoadFromUrl {
    public static Logger logger = LogManager.getLogger(MyDownLoadFromUrl.class.getName());

    public static void downloadFile(String remoteFilePath, File localFilePath, long allSize, EntityTypeMessages entityTypeMessages) throws UnsupportedEncodingException {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        InputStream inputStream = null;
        try (FileOutputStream fileOutputStream = new FileOutputStream(localFilePath)) {
            urlfile = new URL(URLDecoder.decode(remoteFilePath, "UTF-8"));
            httpUrl = (HttpURLConnection) urlfile.openConnection();
            httpUrl.connect();
            inputStream = httpUrl.getInputStream();
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream(fileOutputStream);
            int len = 2048;
            long unitProgress = 0;
            long unit = allSize / 100;//将文件大小分成100分，每一分的大小为unit
            long progress = 0;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
                progress += len;
                long temp = progress / unit;
                if (temp >= 1 && temp > unitProgress && temp % 10 == 0) {
                    unitProgress = temp;//保存当前百分比
                    sender(entityTypeMessages, "正在下载中..." + unitProgress + "%");
                }
            }
            bos.flush();
        } catch (Exception e) {
            logger.error("此目录不存在:\t" + localFilePath.getPath() + "源路径字符串为:\t" + entitySystemProperties.getSystemDir("0"));
            logger.error(e.getMessage(), e);
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (httpUrl != null) {
                    httpUrl.disconnect();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            System.gc();
        }
    }

    public static void downloadFile(String remoteFilePath, File localFilePath, long allSize) throws UnsupportedEncodingException {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        InputStream inputStream = null;
        try (FileOutputStream fileOutputStream = new FileOutputStream(localFilePath)) {
            urlfile = new URL(URLDecoder.decode(remoteFilePath, "UTF-8"));
            httpUrl = (HttpURLConnection) urlfile.openConnection();
            httpUrl.connect();
            inputStream = httpUrl.getInputStream();
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream(fileOutputStream);
            int len = 2048;
            long unitProgress = 0;
            long unit = allSize / 100;//将文件大小分成100分，每一分的大小为unit
            long progress = 0;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
                progress += len;
                long temp = progress / unit;
                if (temp >= 1 && temp > unitProgress && temp % 10 == 0) {
                    unitProgress = temp;//保存当前百分比
                }
            }
            bos.flush();
        } catch (Exception e) {
            logger.error("此目录不存在:\t" + localFilePath.getPath() + "源路径字符串为:\t" + entitySystemProperties.getSystemDir("0"));
            logger.error(e.getMessage(), e);
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (httpUrl != null) {
                    httpUrl.disconnect();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            System.gc();
        }
    }
}
